#!/usr/bin/php
<?php
$lockout_minutes=30;
$lockout_recycle=60;
$glycol_probe = 4;

$connect = mysql_connect("localhost","beerwatch","beerwatch") or die(mysql_error());
$db = mysql_select_db("beerwatch",$connect);
$sql = "SELECT count(*) from glycol_status";
$result = mysql_query($sql,$connect);
$row = mysql_fetch_row($result);
if ($row[0] == 0) {
	$sql = "INSERT into glycol_status (glycol_on) values (0)";
	$result = mysql_query($sql,$connect);
}
while (1) {
	$sql = "SELECT sum(requests) from glycol_requests";
	$result = mysql_query($sql,$connect);
	$row = mysql_fetch_row($result);
	if ($row[0] > 0) {
		//before we turn it on lets see if we have been running
		$sql = "SELECT * from glycol_status";
		$re = mysql_query($sql,$connect);
		$r = mysql_fetch_row($re);
		//go into glycol_status 3 if the glycol is warmer then what we are trying to chill
		$temp=100;
		while(1) {
                	$temp = exec("/usr/bin/gettemp $glycol_probe");
	                $temp = trim($temp);		
			if ($temp != "") {
				break;
			} else {
				sleep(4);
			}
		}
		$sql = "select temp from temps where off=0 order by temp asc limit 1";
		$res = mysql_query($sql,$connect);
		$ro = mysql_fetch_row($res);
		echo "T: $temp $ro[0]\n";
		if ($temp > $ro[0]) {
			echo "$temp > $ro[0]\n";
			//lockout 3
			echo "Lockout\n";
			$sql = "UPDATE glycol_status set glycol_on=3";
			$res = mysql_query($sql,$connect);
			$fp = fopen("/sys/class/gpio/gpio17/value","w");
        		fwrite($fp,"0\n");
        		fclose($fp);
		} else {

			if ($r[0] == 1) {
				//we are on, lets see how many minutes we have been running
				$now = date("Y-m-d H:i:s");
				$start = strtotime($r[1]);
				$now = strtotime($now);
				$minutes = round(abs($now - $start) / 60,2);
				if ($minutes >= $lockout_minutes) {
					//put it in lockout
					echo "Lockout 2\n";
					$sql = "UPDATE glycol_status set glycol_on=2";
					$res = mysql_query($sql,$connect);
					$fp = fopen("/sys/class/gpio/gpio17/value","w");
		        		fwrite($fp,"0\n");
			        	fclose($fp);
					echo 2 ."\n";
				}
			}
			if ($r[0] == 2) {
				//we are in lockout, if it has been 60 minutes or more, take us out of lockout
				$now = date("Y-m-d H:i:s");
				$start = strtotime($r[1]);
				$now = strtotime($now);
				$minutes = round(abs($now - $start) / 60,2);
				echo $minutes ."\n";
				if ($minutes >= $lockout_recycle) {
					echo "Lockout Remove\n";
					$sql = "UPDATE glycol_status set glycol_on=1,start_time=now()";
					$res = mysql_query($sql,$connect);
	        	                $fp = fopen("/sys/class/gpio/gpio17/value","w");
        	        	        fwrite($fp,"1\n");
	                	        fclose($fp);				
				}			
			}	
			if ($r[0] == 3) {
				//shut it off and run again.
                                $sql = "UPDATE glycol_status set glycol_on=1,start_time=now()";
                                $res = mysql_query($sql,$connect);
                                $fp = fopen("/sys/class/gpio/gpio17/value","w");
                                fwrite($fp,"1\n");
                       		fclose($fp);				
			}
			if ($r[0] == 0) {
				echo "Starting\n";
				$sql = "UPDATE glycol_status set glycol_on=1,start_time=now()";
				$res = mysql_query($sql,$connect);
				$fp = fopen("/sys/class/gpio/gpio17/value","w");
        			fwrite($fp,"1\n");
        			fclose($fp);
			}
		}
	} else {
		$fp = fopen("/sys/class/gpio/gpio17/value","w");
        	fwrite($fp,"0\n");
        	fclose($fp);
		$sql = "UPDATE glycol_status set glycol_on=0,start_time=now()";
		$res = mysql_query($sql,$connect);
	}
sleep(20);
}
?>
